import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

// 静态导入组件
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import Dashboard from '@/views/Dashboard.vue'

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { title: '注册', requiresAuth: false }
  },
  {
    path: '/',
    component: Dashboard,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页', requiresAuth: true }
      },
      {
        path: 'user/list',
        name: 'UserList',
        component: () => import('@/views/UserList.vue'),
        meta: { title: '用户列表', requiresAuth: true }
      },
      {
        path: 'role/list',
        name: 'RoleList',
        component: () => import('@/views/RoleList.vue'),
        meta: { title: '角色管理', requiresAuth: true }
      },
      {
        path: 'athlete/list',
        name: 'AthleteList',
        component: () => import('@/views/AthleteList.vue'),
        meta: { title: '运动员管理', requiresAuth: true }
      },
      {
        path: 'judge/list',
        name: 'JudgeList',
        component: () => import('@/views/JudgeList.vue'),
        meta: { title: '裁判员管理', requiresAuth: true }
      },
      {
        path: 'event/list',
        name: 'EventList',
        component: () => import('@/views/EventList.vue'),
        meta: { title: '比赛项目管理', requiresAuth: true }
      },
      {
        path: 'registration/list',
        name: 'RegistrationList',
        component: () => import('@/views/RegistrationList.vue'),
        meta: { title: '报名管理', requiresAuth: true }
      },
      {
        path: 'registration/audit',
        name: 'RegistrationAudit',
        component: () => import('@/views/RegistrationAudit.vue'),
        meta: { title: '报名审核', requiresAuth: true }
      },
      {
        path: 'event/checkin',
        name: 'EventCheckin',
        component: () => import('@/views/EventCheckin.vue'),
        meta: { title: '赛事检录', requiresAuth: true }
      },
      {
        path: 'score/entry',
        name: 'ScoreEntry',
        component: () => import('@/views/ScoreEntry.vue'),
        meta: { title: '成绩录入', requiresAuth: true }
      },
      {
        path: 'score/query',
        name: 'ScoreQuery',
        component: () => import('@/views/ScoreQuery.vue'),
        meta: { title: '成绩查询', requiresAuth: true }
      },
      {
        path: 'score/ranking',
        name: 'ScoreRanking',
        component: () => import('@/views/ScoreRanking.vue'),
        meta: { title: '排名统计', requiresAuth: true }
      },
      {
        path: 'notice',
        name: 'NoticeManagement',
        component: () => import('@/views/NoticeManagement.vue'),
        meta: { title: '公告管理', requiresAuth: true }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    redirect: '/'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()
  
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 运动会管理系统` : '运动会管理系统'
  
  // 如果是登录或注册页面，直接放行
  if (!to.meta.requiresAuth) {
    // 如果已经登录，重定向到首页
    if (userStore.isLoggedIn && (to.name === 'Login' || to.name === 'Register')) {
      next('/')
      return
    }
    next()
    return
  }
  
  // 检查是否登录
  if (!userStore.isLoggedIn) {
    ElMessage.warning('请先登录')
    next('/login')
    return
  }
  
  // 如果没有用户信息，获取用户信息
  if (!userStore.userInfo) {
    const result = await userStore.getUserInfo()
    if (!result.success) {
      next('/login')
      return
    }
  }
  
  next()
})

export default router